import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import AutoImport from 'unplugin-auto-import/vite';
import basicSsl from "@vitejs/plugin-basic-ssl";

// https://vitejs.dev/config/
export default defineConfig(async ({ command, mode }) => {
    // 根据当前工作目录中的 `mode` 加载 .env 文件
    // 设置第三个参数为 '' 来加载所有环境变量，而不管是否有 `VITE_` 前缀。
    const env = loadEnv(mode, process.cwd(), '');
    console.log(command);
    console.log(mode);
    return {
        plugins: [
            vue(),
            Components({
                resolvers: [VantResolver()],
            }),
            AutoImport({
                include: [
                    /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
                    /\.vue$/,
                    /\.vue\?vue/, // .vue
                ],
                imports: [
                    'vue',
                    'vue-router'
                    // 'uni-app',
                ],
                dts: 'typings/auto-imports.d.ts',
            }),
            // 启动HTTP服务
            // basicSsl()
        ],
        resolve: {
            alias: [
                { find: 'src', replacement: resolve(__dirname, 'src') },
                { find: '~@', replacement: resolve(__dirname, 'src') }
            ],
            extensions: ['.js', '.vue', 'css', 'less', '.json', '.mjs', '.ts', '.jsx', '.tsx']
        },
        css: {
            preprocessorOptions: {
                less: {
                    javascriptEnabled: true,
                    additionalData: `@import "${resolve(__dirname, 'src/assets/less/global.less')}";`
                }
            }
        },
        define: {
            'process.env': { 'NODE_ENV': env.NODE_ENV }
        },
        base: './',
        esbuild: {
            drop: ['console', 'debugger']
        },
        //打包配置
        build: {},
        server: {
            port: 8383,
            host: '0.0.0.0',
            proxy: {
                '/ztApi': {
                    target: 'http://192.168.1.17:8117',
                    ws: true,
                    changOrigin: true,
                    rewrite: path => path.replace(/^\/ztApi/, '')
                },
                '/remote': {
                    // 'target': 'http://localhost:8848',
                    target: 'http://192.168.1.74:8848',
                    // 'target': 'https://mini.sunlife.com.cn',
                    ws: true,
                    changOrigin: true,
                    rewrite: path => path.replace(/^\/remote/, '')
                },

            }
        }
    };
});
